package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-08-08
 * Time: 22:05
 */
@RestController
@RequestMapping("/aaa/bbb")
public class TestController {

    @RequestMapping(path = {"/ccc/say_hi", "/log"}, method = {RequestMethod.POST, RequestMethod.GET})
    public String sayHi(@RequestParam(value = "n", required = false) String name, int id) {
        return name + "  :  " + id;
    }
    @RequestMapping("get_json")
    public String getJSON(@RequestBody User user) {
        return user.toString();
    }
    @RequestMapping("/get_url/{i}")
    public String getURL(HttpServletRequest request, String url, @PathVariable("i") int id) {
        return request.getRequestURL().toString() + "/" + id;
    }
    @RequestMapping("/save_file")
    public void saveFile(@RequestPart(name = "fileName") MultipartFile file) throws IOException {
        String path = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX).getPath() +  "/";
        //获取唯一的id
        String id = UUID.randomUUID().toString();
        //获取文件后缀
        String originName = file.getOriginalFilename();
        String suffix = originName.substring(originName.lastIndexOf("."));
        path += id + suffix;
        System.out.println(path);
        file.transferTo(new File(path));
    }

    @RequestMapping("get_cookie")
    public String getCookie(@CookieValue(value = "n", required = false) String name) {
        //巴拉巴拉
        return name;
    }
    @RequestMapping("set_session")
    public String setSession(HttpSession session) {
        if(session != null) {
            session.setAttribute("name",  "大帅哥");
            session.setAttribute("id",  666);
        }
        return "存储成功！";
    }

    @RequestMapping("/get_session")
    public String getSession(@SessionAttribute String name, @SessionAttribute int id) {
        return name + " : " + id;
    }

    @RequestMapping("/get_header")
    public String getHeader(@RequestHeader("User-Agent") String userAgent) {
        return userAgent;
    }



}
